package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.dto.SetmealDto;
import com.reggie.entity.Dish;
import com.reggie.entity.Setmeal;
import com.reggie.entity.SetmealDish;
import com.reggie.entity.ShoppingCart;
import com.reggie.service.DishService;
import com.reggie.service.SetmealDishService;
import com.reggie.service.SetmealService;
import com.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name) {
        Page<SetmealDto> setmealDtoPage = setmealService.pageSetmeal(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveSetmeal(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> findById(@PathVariable Long id) {
        SetmealDto byId = setmealService.findById(id);
        return R.success(byId);
    }

    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改成功");
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam("ids") List<Long> ids) {
        setmealService.deleteSetmeal(ids);
        return R.success("删除成功");
    }

    @PostMapping("/status/{statu}")
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> updateStatus(@PathVariable int statu, @RequestParam("ids") List<Long> ids) {
        if (ids != null) {
            LambdaUpdateWrapper<Setmeal> setmealLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            setmealLambdaUpdateWrapper.in(Setmeal::getId, ids);
            setmealLambdaUpdateWrapper.set(Setmeal::getStatus, statu);
            setmealLambdaUpdateWrapper.set(Setmeal::getUpdateTime, LocalDateTime.now());
            //删除购物车里对应的套餐
            if (statu == 0) {
                LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
                shoppingCartLambdaQueryWrapper.in(ShoppingCart::getSetmealId, ids);
                shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
            }
            setmealService.update(setmealLambdaUpdateWrapper);
        }
        return R.success("操作成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#categoryId+':'+#status")
    public R<List<Setmeal>> list(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(categoryId != null, Setmeal::getCategoryId, categoryId);
        setmealLambdaQueryWrapper.eq(status != null, Setmeal::getStatus, status);
        List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);
        return R.success(list);
    }

    @GetMapping("/dish/{id}")
    public R<List<Dish>> getDishes(@PathVariable Long id) {
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
        List<Long> dishIds = new ArrayList<>();
        list.forEach(setmealDish ->
                dishIds.add(setmealDish.getDishId())
        );
        List<Dish> dishes = dishService.listByIds(dishIds);
        return R.success(dishes);
    }
}
